//
//  HxChatManager.swift
//  Runner
//
//  Created by 123456 on 2023/6/16.
//

import UIKit
import Hyphenate
import HelpDesk
import Flutter


class HxChatManage: NSObject, HDChatManagerDelegate,FlutterStreamHandler{

  
    
  
    public func register(registrar:FlutterPluginRegistrar) {
        
        let instance = HxChatManage.shared
        let eventChannel = FlutterEventChannel(name: "cpd_easemob_cec_event", binaryMessenger: registrar.messenger())
        eventChannel.setStreamHandler(instance)
    }
   

    
    func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
        sink = events
        return nil
    }
    
    func onCancel(withArguments arguments: Any?) -> FlutterError? {
        sink = nil
        return nil
    }
    
    
    
    
    static let shared = HxChatManage()
    
    var eventChannel:FlutterEventChannel?
    
    var sink:FlutterEventSink?
    
    
    
    
    deinit {
        HDClient.shared().chatManager.remove(self)
    }
    
    
    public func setEventMessage(){
        HDClient.shared().chatManager.add(self, delegateQueue: nil)
    }
    
    
//    list<EMMessage>
    ///收到普通消息,格式:<HDMessage *>
    func messagesDidReceive(_ aMessages: [Any]!) {
        if sink != nil{
            sink!(["onMessage":MessageManage.messagesToListMap(messages: aMessages)])
        }
        
    }
    
    ///收到命令消息,格式:<HDMessage *>，命令消息不存数据库，一般用来作为系统通知，例如留言评论更新，
    ///会话被客服接入，被转接，被关闭提醒
    func cmdMessagesDidReceive(_ aCmdMessages: [Any]!) {
        if sink != nil{
            sink!( ["onCmdMessage":MessageManage.messagesToListMap(messages: aCmdMessages)])
        }
    }
    
    /////消息的状态修改，一般可以用来刷新列表，显示最新的状态
    func messageStatusDidChange(_ aMessage: HDMessage!, error aError: HDError!) {
        if sink != nil{
            sink!( ["onMessageStatusUpdate":MessageManage.messageToMap(message: aMessage)])
        }
        
    }
    
    ///发送消息后，会调用，可以在此刷新列表，显示最新的消息
    func messageAttachmentStatusDidChange(_ aMessage: HDMessage!, error aError: HDError!) {
        if sink != nil{
            sink!(
                ["onMessageSent":MessageManage.messageToMap(message: aMessage)]
            )
        }
    
    }
    
    
    
    //是否链接
//        if (event["onConnected"] != null) {
//          listener.onConnected?.call();
//        }
   //是未链接
//        if (event["onDisconnected"] != null) {
//          listener.onDisconnected?.call(event["onDisconnected"].toString());
//        }
      
     //    list<EMMessage>
     ///收到普通消息,格式:<HDMessage *>
//        if (event["onMessage"] != null) {
//          listener.onMessage?.call(event["onMessage"] as List);
//        }
    ///收到命令消息,格式:<HDMessage *>，命令消息不存数据库，一般用来作为系统通知，例如留言评论更新，
    ///会话被客服接入，被转接，被关闭提醒
//        if (event["onCmdMessage"] != null) {
//          listener.onCmdMessage?.call(event["onCmdMessage"] as List);
//        }
    
    /////消息的状态修改，一般可以用来刷新列表，显示最新的状态
//        if (event["onMessageStatusUpdate"] != null) {
//          listener.onMessageStatusUpdate?.call();
//        }
    ///发送消息后，会调用，可以在此刷新列表，显示最新的消息
//        if (event["onMessageSent"] != null) {
//          listener.onMessageSent?.call();
//        }
    
    
    
}




